#!/bin/bash

source /home/shell/util.sh

if [ "$(ostype)" == "Ubuntu" ]; then
    apt -y install make gcc automake zlib1g-dev openssl libssl-dev libpcre3 libpcre3-dev
else
    yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel
fi


printInfo "nginx $(date +%Y)年版本："
curl -sL http://nginx.org/download|grep $(date +%Y)|grep asc|grep -v zip|grep -v patch|perl -pe 's@.*>{1}.*nginx\-(.*?).tar.gz.asc</a>{1}.*@\1@g'
while [ true ]; do
    read -p "选择nginx版本：" version
    if [[ -z "${version}" ]]; then
        printFail "请重新输入版本！"
        continue;
    else
        break;
    fi
done

if [ ! -f "/usr/local/src/nginx.tar.gz" ];then
    download /usr/local/src/nginx.tar.gz http://nginx.org/download/nginx-${version}.tar.gz
fi
tar -zxvf /usr/local/src/nginx.tar.gz -C /usr/local/src
rm -rf /usr/local/src/nginx.tar.gz
mv /usr/local/src/nginx-${version} /usr/local/src/nginx
cd /usr/local/src/nginx
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/webapps/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads \
--with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module \
--with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module \
--with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module \
--with-stream_ssl_preread_module
make && make install

mkdir -p /var/log/webapps/nginx/
sed -ri "s@#error_log  logs/error.log;@error_log  /var/log/webapps/nginx/admin_error.log;@" /etc/nginx/nginx.conf
sed -ri "s@#access_log  logs/access.log  main;@access_log /var/log/webapps/nginx/access.log;@" /etc/nginx/nginx.conf
sed -ri "s@#user  nobody;@user  root;@" /etc/nginx/nginx.conf

# setsebool -P httpd_read_user_content 1
useradd -s /sbin/nologin -M nginx
mkdir -p /var/cache/nginx
rm -rf /opt/nginx.tar.gz

bash /home/shell/serviced-install.sh<<EOF
2
1

nginx
nginx -c /etc/nginx/nginx.conf
/bin/sleep 0.1
nginx -c /etc/nginx/nginx.conf -s reload
nginx -c /etc/nginx/nginx.conf -s stop

EOF

firewall-addport 80

nginx_conf=`nginx -t 2>&1|grep test|perl -pe "s/.*file\s?(.*?)\stest.*/\1/g"`;

printInfo "=======================nginx======================="
printInfo "错误日志：/var/log/webapps/nginx/admin_error.log"
printInfo "访问日志：/var/log/webapps/nginx/access.log"
printInfo "配置文件：${nginx_conf}"
printInfo "Web地址：http://$host_addr/"
nginx -V
printInfo "=======================nginx======================="